/**
 * @FileName: PromotionController.java
 * @Author
 * @Description:
 * @Date 2016年6月27日 下午2:31:12
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.admin.recommend;

import cn.hpclub.server.controller.admin.BaseAdminController;
import cn.hpclub.server.model.recommend.Recommend;
import cn.hpclub.server.model.visit.VisitInfo;
import cn.hpclub.server.service.ds.JobByBusinessCount;
import cn.hpclub.server.service.recommend.RecommendService;
import cn.hpclub.server.service.visit.VisitService;
import cn.hpclub.server.util.ExcelRenderUtil;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.ObjectUtil;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @FileName: RecommendController.java
 * @Author zhl
 * @Description:推荐有奖后台Controller
 * @Date 2016年8月10日 下午2:38:57
 * @CopyRight ZTE Corporation
 */
public class RecommendController extends BaseAdminController<Recommend>{

    private static Logger logger = LoggerFactory.getLogger(RecommendController.class);

    public void list(){
        List<Record> list = InterfaceUtil.getAllCity();
        setAttr("city_list", list);// 加载地市列表

        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        int city_id = Integer.parseInt(getPara("city_id", "-1"));

        if(city_id == -1){
            int org_id = InterfaceUtil.getOrgId();
            Record record = InterfaceUtil.getInfoByOrg(org_id);
            if(ObjectUtil.isNotEmpty(record) && ObjectUtil.isNotEmpty(record.getInt("city_id"))){
                city_id = record.getInt("city_id");
            }
        }
        if((!searchInfo.contains("city_id") || "-1".equals(searchInfo.get("city_id"))) && city_id != -1){
            searchInfo.put("city_id", String.valueOf(city_id));
        }
        if("-1".equals(searchInfo.get("city_id"))){
            searchInfo.remove("city_id");
        }
        int city_id_attr = city_id;
        if(StrKit.notBlank(searchInfo.get("city_id"))){
            city_id_attr = Integer.parseInt(searchInfo.get("city_id"));
        }
        setAttr("city_id", city_id_attr);
        findByPageForSearch("SELECT r.id,r.user_id, r.name,r.phone,r.first_service_time,m.name as recommender_name,r.user_award,r.recommender_award,r.create_time ", new RecommendService().service.getSqlExceptSelect(searchInfo));
        Page<Recommend> pager = getAttr(PAGER);
        List<Recommend> recommendList = pager.getList();
        for (Recommend obj : recommendList) {
            Date first_service_time = obj.getDate("first_service_time");
            if(ObjectUtil.isEmpty(first_service_time)){
                int record_user_id = obj.getInt(Recommend.USER_ID);
                int id = obj.getInt(Recommend.ID);
                Record firstRecord = new RecommendService().service.getFirstServiceInfo(record_user_id);
                if(ObjectUtil.isNotEmpty(firstRecord)){
                    first_service_time = firstRecord.getDate("time");
                    /*String updateSql = "update recommend set first_service_time = ? where id = ?";
                    Db.update(updateSql, new Object[]{first_service_time, id});*/
                    obj.set("first_service_time",first_service_time);
//                    obj.update();
                }
            }
        }
        Db.batch("update recommend set first_service_time = ? where id = ?","first_service_time,id",recommendList,recommendList.size());
        setAttr(PAGER, pager);
        render("/admin/recommend_detail.tpl");
    }

    public void exportAll(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        if("-1".equals(searchInfo.get("city_id"))){
            searchInfo.remove("city_id");
        }
        SXSSFWorkbook workbook = new RecommendService().service.getExcel(searchInfo);
        render(new ExcelRenderUtil(workbook, "推荐有奖"));
    }

}
